/*
 *  Copyright (C) 2010 takaji
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * BalanceSheetView.java
 *
 * Created on May 2, 2010, 5:27:27 PM
 */
package dakside.hacc.reports;

import com.lowagie.text.DocumentException;
import org.dakside.duck.helpers.SwingHelper;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;

/**
 *
 * @author takaji
 */
public class XHTMLReport extends javax.swing.JPanel {

    private static final Logger logger = Logger.getLogger(XHTMLReport.class.getName());

    /** Creates new form BalanceSheetView */
    public XHTMLReport() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fSScrollPane1 = new org.xhtmlrenderer.simple.FSScrollPane();
        xHTMLPanel1 = new org.xhtmlrenderer.simple.XHTMLPanel();

        fSScrollPane1.setName("fSScrollPane1"); // NOI18N

        xHTMLPanel1.setName("xHTMLPanel1"); // NOI18N

        javax.swing.GroupLayout xHTMLPanel1Layout = new javax.swing.GroupLayout(xHTMLPanel1);
        xHTMLPanel1.setLayout(xHTMLPanel1Layout);
        xHTMLPanel1Layout.setHorizontalGroup(
            xHTMLPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 383, Short.MAX_VALUE)
        );
        xHTMLPanel1Layout.setVerticalGroup(
            xHTMLPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 283, Short.MAX_VALUE)
        );

        fSScrollPane1.setViewportView(xHTMLPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fSScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fSScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.xhtmlrenderer.simple.FSScrollPane fSScrollPane1;
    private org.xhtmlrenderer.simple.XHTMLPanel xHTMLPanel1;
    // End of variables declaration//GEN-END:variables

    protected void render(InputStream input) {
        try {
            if (input != null) {
                xHTMLPanel1.setDocument(input, "/");
            }
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }

    protected Document getDocument() {
        return xHTMLPanel1.getDocument();
    }

    /**
     * Export current content to PDF file
     * @param filename
     */
    protected void exportToPDF(String filename) {
        File f = SwingHelper.askSave(filename);
        if (f != null) {
            try {
                ITextRenderer renderer = new ITextRenderer();
                renderer.setDocument(getDocument(), "/");
                renderer.layout();
                renderer.createPDF(new FileOutputStream(f));
                //XXX remove debug info
                logger.log(Level.INFO, "Report has been generated to {0}", f.getAbsolutePath());
            } catch (DocumentException ex) {
                logger.log(Level.SEVERE, "Cannot generate report", ex);
            } catch (FileNotFoundException ex) {
                logger.log(Level.SEVERE, "Cannot write to file", ex);
            }
        }
    }
}
